package app.netant.galgame.yuinya.net;

import org.jsoup.select.Elements;

import app.netant.NetAntBase;
/**
 * 喵窝分析
 * @author JiaJ
 * @date 2018年6月26日
 *
 */
public class YuiNyaNetDetail extends NetAntBase {
	
	private YuiNyaNetEntity entity;
	
	public YuiNyaNetDetail(String netSite) {
		super(netSite);
	}
	
	public void setEntity(YuiNyaNetEntity entity){
		this.entity = entity;
	}
	
	/**
	 * 标签分析
	 */
	private void analyseTags(){
		
		Elements activePart = this.document.select("section.container article.article-content p");
		
		for(int i=0;i<activePart.size();i++){
			
			String str = activePart.get(i).html().replaceAll("<br>", "\n").replaceAll("<br/>", "\n");
			String curStr = cutString(str, '<', '>').trim();
			
			if(curStr.startsWith("游戏名称")){
				
				String[] tags = curStr.split("\n");
				
				for(String tag:tags){
					int index = tag.indexOf("：");
					
					if(index>0 && index <tag.length()-1){
						this.entity.setTags(tag.substring(0,index),tag.substring(index+1));
					}				

				}

			}
			
		}
		
	}
	
	/**
	 * 磁链分析
	 */
	private void analyseThunder(){
		
		Elements activePart = this.document.select("section.container article.article-content p");
		
		for(int i=0;i<activePart.size();i++){
			
			String str = activePart.get(i).html().replaceAll("<br>", "\n").replaceAll("<br/>", "\n");
			String curStr = cutString(str, '<', '>').trim();
			
			if(curStr.startsWith("磁力链接")){
				
				if(curStr.length()>=6){
					this.entity.setGameThunder(curStr.substring(6).trim());
				}
				
			}
			
		}
		
	}
	
	/**
	 * 网盘分析
	 */
	private void analyseBaiduYun(){
		
		Elements activePart = this.document.select("section.container article.article-content p");
		
		for(int i=0;i<activePart.size();i++){
			
			String str = activePart.get(i).html().replaceAll("<br>", "\n").replaceAll("<br/>", "\n");
			String curStr = cutString(str, '<', '>').trim();
			
			if(curStr.startsWith("网盘下载")){
				
				String[] tags = curStr.split("\n");
				
				int m = 0;
				
				for(int j=0;j<tags.length;j++){
					
					String active = tags[j].trim();
					
					if(active.startsWith("文件名")){
						if(active.length()>5){							
							this.entity.setFileName(active.substring(4).trim());
						}
					}
					
					if(active.startsWith("解压密码")){
						if(active.length()>6){
							this.entity.setFilePwd(active.substring(5).trim());
						}
					}
					
					if(active.startsWith("百度网盘")){
						m = j;
					}
				}
				
				String url = "";
				for(int a=m;a<tags.length;a++){
					String active = tags[a].trim();
					if(active.startsWith("http")){
						url += ","+active;
					}
				}
				if(url.length()>1){
					url = url.substring(1);
				}
				
				this.entity.setFileUrl(url);
				
			}
			
		}
		
	}
	
	/**
	 * 介绍分析
	 */
	private void analyseMemo(){
		
		Elements activePart = this.document.select("section.container article.article-content blockquote");
		
		if(activePart.size()>0){
			
			String str = activePart.get(0).html().replaceAll("<br>", "\n")
												 .replaceAll("<br/>", "\n")
												 .replaceAll("</p>", "\n");
			String curStr = cutString(str, '<', '>').trim();
			
			this.entity.setGameMemo(this.getString(curStr));
			
		}
		
	}
	
	/**
	 * 分析数据
	 */
	public void analyse(){
		
		this.analyseTags();
		this.analyseMemo();
		this.analyseBaiduYun();
		this.analyseThunder();
		
	}
	
	public static void main(String[] args) {
		YuiNyaNetDetail detail = new YuiNyaNetDetail("http://yui-nya.com/games/20844-1000303");
		//detail.analyseTags();
		//detail.analyseMemo();
		//detail.analyseThunder();
		detail.analyseBaiduYun();
		
	}

}
